<?php 
class CBlogController extends CController{

	protected $m_arrobjUsers;
	protected $m_objUser;
	protected $m_objFaq;
	
	function __construct() {
		parent::__construct();
		return true;
	}
	
	function create() {
		parent::create();
	}
	
	function initialize() {
		parent::initialize();
	}
	
	function execute() {		
		
		switch( $this->m_strAction ) {
			
			case NULL:			
			case 'view_blogs':
				$this->handleViewBlogs();
				break;
				
			case 'add_edit_blog':
				$this->handleAddEditBlog();
				break;
							
			case 'insert_update_blog':
					$this->handleInsertUpdateBlog();
				break;
			case 'update_blog_action':
				$this->handleUpdateBlogAction();
				break;
			
			case 'view_blog_in_detail':
					$this->handleViewBlogInDetail();
			break;
					
			default:
				// Empty default case
				
		}
	}

	/*************************************
	********** HANDLE FUNCTIONS **********
	*************************************/
		
	function handleViewBlogs() {
		$this->m_arrobjBlogs = CBlogs::fetchBlogs( $this->m_objDatabase );	
		$this->displayBlogs();
	}
	
	function handleAddEditBlog() {
		include( "Liabraries/fckeditor/fckeditor.php");
	
		$this->m_objFCKeditor = new FCKeditor('blog[description]') ;
		$this->m_objFCKeditor->BasePath = SITE_PATH . '/Liabraries/fckeditor/';
		$this->m_objFCKeditor->Width  = '100%';
		$this->m_objFCKeditor->Height = '500';
	
		$this->m_objBlog = new CBlog();
		
		if( true == isset( $_REQUEST['id'] ) ) {
			$this->m_objBlog = CBlogs::fetchBlogById( $_REQUEST['id'], $this->m_objDatabase );
			$this->m_objFCKeditor->Value  = $this->m_objBlog->getDescription();
		}
		
	
		$this->displayAddEditBlog();
	}
	
	
	function handleInsertUpdateBlog() {
		include( LIABRARIES_PATH . "Common/common.lib.php");
		
		switch( true ) {
			default:
				$arrmixFieldsValues = $this->m_objData->getPostData( array( 'blog' ) );
		
				$this->m_objBlog = CBlogs::setValues( 'CBlog', $arrmixFieldsValues );
				$this->m_objBlog->setCreatedBy($_SESSION['udeal.in']['login_user_id']);
				$this->m_objBlog->setCreatedOn(date('Y-m-d'));
				 $width_array = array("236");
				$height_array = array("126");
				$strPath = MOUNT_PATH.'BlogImages/';
				$path_array = array($strPath);
				$original_1=resize_multiple_images($_FILES['file'],$width_array,$path_array,$original_img_path,$height_array);
				$this->m_objBlog->setThumbnail($original_1);
				
				//	display($original_1);die;
				/* if( false == $this->m_objFaq->validate( $arrmixFieldsValues, array_keys( $arrmixFieldsValues ) ) ) {
					break;
				} */
				
				if( false == is_null( $this->m_objBlog->getId() ) && '' != trim( $this->m_objBlog->getId() ) ) {
						
					if( false == $this->m_objBlog->update( $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to update Blog.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'Blog update successfully. ' );
					}
				} else {
					
					if( false == $this->m_objBlog->insert( $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to create Blog.' );
					} else {

						$this->m_objErrorMessage->setSuccessMessage( 'Blog created successfully. ' );
					}
				}
				redirect( '/admin/blog' );
		}
 		redirect( '/admin/blog/add_edit_blog' );
	}

	function handleUpdateBlogAction() {
	
		switch( true ) {
			default:
	
				$strAction = $this->m_objData->getPostData( array( 'blog', 'action' ) );
				if( false == isset( $strAction  ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select action ' );
					break;
				}
	
				$arrintBlogIds = $this->m_objData->getPostData( array( 'blog', 'id' ) );
				if( false == valArr( $arrintBlogIds ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select records to delete ' );
					break;
				}
	
				$this->m_objBlog = new CBlog();
				if( false == valObj( $this->m_objBlog, 'CBlog' ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Failed to create object' );
					break;
				}
	
				if( 'delete' == $strAction ) {
					if( true == $this->m_objBlog->deleteBlogsByIds( $arrintBlogIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) deleted successfully.' );
					}
				} else {
					if( true == $this->m_objBlog->updateBlogsActionByIds( $strAction, $arrintBlogIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) updated successfully.' );
					}
				}
					
		}
		redirect( '/admin/blog' );
	}
	
	
	function handleViewBlogInDetail() {
		$intBlogId = $_REQUEST['id'];
		$this->m_objBlog = CBlogs::fetchBlogById( $intBlogId, $this->m_objDatabase );
		$this->m_arrobjComment = CComments::fetchCommentsByBlogId( $intBlogId, $this->m_objDatabase);
		$this->displayBlogInDetail();
	}
	
	/**************************************
	********** DISPLAY FUNCTIONS **********
	**************************************/		
	
	function displayAddEditBlog() {
		$this->display();
	
		$this->m_objSmarty->assign_by_ref( 'fckeditor', $this->m_objFCKeditor );
		$this->m_objSmarty->assign_by_ref( 'blog', $this->m_objBlog );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/blog/view_add_edit_blog.tpl' );
	}
	
	function displayBlogs() {
		$this->display();

		$this->m_objSmarty->assign( 'blogs', $this->m_arrobjBlogs );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/blog/view_blogs.tpl');

	}
	
	function displayBlogInDetail() {
		$this->display();
	
		$this->m_objSmarty->assign( 'blog', $this->m_objBlog );
		$this->m_objSmarty->assign( 'comments', $this->m_arrobjComment );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/comment/view_comments.tpl');
	
	}	
	
	function display() {
		parent::display();
		$this->m_objSmarty->assign( 'leftadminmenu', 'blog' );
		
	}

}
?>